package com.ansion.commonchat.contact.bean;

import com.ansion.basedb.dao.bean.SyncAdapterColumns;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class SyncUser {
	public long mUserId;
	public String mName;
	public String mPhone;

	public long mContactId;
	public long mRawContactId;
	
	public Map<String,String> mimeMaps = new HashMap<String,String>();

	public SyncUser(long userId) {
		mUserId = userId;
	}
	
	public SyncUser(int rowContactId) {
		mRawContactId = rowContactId;
	}

	public SyncUser(long userId, String name, String phone) {
		this.mUserId = userId;
		this.mName = name;
		this.mPhone = phone;
	}

	public boolean isValid() {
		if (mimeMaps.size()!= SyncAdapterColumns.MIME_COUNT) {
			return false;
		}
		
		String mimeProfile= SyncAdapterColumns.MIME_PROFILE;
		String mimeChat= SyncAdapterColumns.MIME_CHAT;
		String mimeVideocall= SyncAdapterColumns.MIME_CALL_VIDEO;
		String mimeVoicecall= SyncAdapterColumns.MIME_CALL_VOICE;
		
		boolean mimeTypeMatch= mimeMaps.containsKey(mimeProfile)
				&& mimeMaps.containsKey(mimeChat)
				&& mimeMaps.containsKey(mimeVideocall)
				&& mimeMaps.containsKey(mimeVoicecall);
		if (!mimeTypeMatch) {
			return false;
		}
		
		if (mUserId<=0) {
			return false;
		}
		
		if (mimeMaps.containsKey(mimeChat)) {
			String chatLabel=mimeMaps.get(mimeChat);
			if (chatLabel!=null&&!chatLabel.contains(String.valueOf(mUserId))) {
				return false;
			}
		}
		
		return true;
		
//		String chatTxt=HelperFunc.getChatLabel();
//		String voipcallTxt=HelperFunc.getVoipCallLabel();
//		
//		boolean isTxtMatch=chatTxt.equals(mimeMaps.get(mimeChat))&&voipcallTxt.equals(mimeMaps.get(mimeVoipcall));
//		
//		return isTxtMatch;
		
	}

	@Override
	public String toString() {
		return "SyncUser [mUserId=" + mUserId + ", mName=" + mName
				+ ", mPhone=" + mPhone + ", mContactId=" + mContactId
				+ ", mRawContactId=" + mRawContactId + "mimeTypes=("
				+ joinSetValues(mimeMaps) + ")" + "]";
	}

	private static <K,V> String joinSetValues(Map<K,V> a) {
		if (a == null || a.isEmpty()) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		for (Entry<K, V>  entry: a.entrySet()) {
			sb.append("(" + entry.getKey()+","+ entry.getValue()+"),");
		}
		return String.valueOf(sb);
	}

}
